package com.dykj.fireFighting.server.socialUnit.firemanger.service.impl;

import com.dykj.fireFighting.api.socialUnit.devicemanger.entity.PartitionsDevice;
import com.dykj.fireFighting.api.socialUnit.devicemanger.service.IPartitionsDeviceService;
import com.dykj.fireFighting.api.socialUnit.firemanger.entity.CoordinateRelation;
import com.dykj.fireFighting.api.socialUnit.firemanger.entity.HazardSources;
import com.dykj.fireFighting.api.socialUnit.firemanger.entity.HazardSourcesDevice;
import com.dykj.fireFighting.api.socialUnit.firemanger.service.ICoordinateRelationService;
import com.dykj.fireFighting.api.socialUnit.firemanger.service.IFireStationDeviceService;
import com.dykj.fireFighting.api.socialUnit.firemanger.service.IHazardSourcesDeviceService;
import com.dykj.fireFighting.api.socialUnit.firemanger.service.IHazardSourcesService;
import com.dykj.fireFighting.api.socialUnit.firemanger.vo.HazardSourcesDevicedVo;
import com.dykj.fireFighting.common.core.service.impl.BaseServiceImpl;
import com.dykj.fireFighting.common.core.utils.UUIDUtils;
import com.dykj.fireFighting.server.socialUnit.firemanger.mapper.HazardSourcesMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;


import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 消防管理-危险源
 *
 * @Author JacksonTu
 * @Date  2021-06-25 08:34:48
 */
@Slf4j
@DubboService
@Transactional(rollbackFor = Exception.class)
public class HazardSourcesServiceImpl extends BaseServiceImpl<HazardSourcesMapper, HazardSources> implements IHazardSourcesService {
    @Resource
    IHazardSourcesDeviceService hazardSourcesDeviceService;

    @Resource
    ICoordinateRelationService coordinateRelationService;
    @Resource
    IPartitionsDeviceService partitionsDeviceService;
    @Transactional
    @Override
    public Boolean insertHazardSources(HazardSourcesDevicedVo hazardSourcesDevicedVo){
        HazardSources hazardSources=new HazardSources();
        BeanUtils.copyProperties(hazardSourcesDevicedVo,hazardSources);
        hazardSources.setCreateTime(new Date());
        hazardSources.setUpdateTime(new Date());
        String  hazardSourcesId= UUIDUtils.get32UUID();
        hazardSources.setId( hazardSourcesId);
        log.info("危险源写入数据：{}", hazardSources.toString());
        save(hazardSources);
        for(int i=0;i< hazardSourcesDevicedVo.getDevice().size();i++){
            HazardSourcesDevice hazardSourcesDevice=new HazardSourcesDevice();
            hazardSourcesDevice.setDeviceId(hazardSourcesDevicedVo.getDevice().get(i));
            hazardSourcesDevice.setHazardSourcesId(hazardSourcesId);
            hazardSourcesDevice.setType(0);
            hazardSourcesDevice.setId(UUIDUtils.get32UUID());

            hazardSourcesDeviceService.save(hazardSourcesDevice);
        }
        for(int i=0;i<hazardSourcesDevicedVo.getDeviceTradition().size();i++){
            HazardSourcesDevice hazardSourcesDevice=new HazardSourcesDevice();
            hazardSourcesDevice.setDeviceId(hazardSourcesDevicedVo.getDeviceTradition().get(i));
            hazardSourcesDevice.setHazardSourcesId(hazardSourcesId);
            hazardSourcesDevice.setType(1);
            hazardSourcesDevice.setId(UUIDUtils.get32UUID());
            hazardSourcesDeviceService.save(hazardSourcesDevice);
        }
        if(StringUtils.isNotBlank(hazardSourcesDevicedVo.getPartitionId())){
            PartitionsDevice partitionsDevice=new PartitionsDevice();
            partitionsDevice.setId(UUIDUtils.get32UUID());
            partitionsDevice.setType(102);
            partitionsDevice.setDeviceId(hazardSources.getId());
            partitionsDevice.setDeviceName(hazardSources.getName());
            partitionsDevice.setY(hazardSourcesDevicedVo.getLat());
            partitionsDevice.setX(hazardSourcesDevicedVo.getLng());
            partitionsDevice.setPartitionId(hazardSourcesDevicedVo.getPartitionId());
            log.info("插入分区坐标关系表：{}",partitionsDevice.toString());
            partitionsDeviceService.insertPoint(partitionsDevice);
        }else {
            //关系表
            CoordinateRelation coordinateRelation = new CoordinateRelation();
            coordinateRelation.setLng(hazardSourcesDevicedVo.getLng());
            coordinateRelation.setLat(hazardSourcesDevicedVo.getLat());
            coordinateRelation.setTableId(hazardSourcesId);
            coordinateRelation.setId(UUIDUtils.get32UUID());
            coordinateRelation.setUnitId(hazardSourcesDevicedVo.getUnitId());
            coordinateRelation.setTableType("1");
            log.info("插入坐标关系表：{}", coordinateRelation.toString());
            coordinateRelationService.save(coordinateRelation);
        }
        return true;
    }

    /**
     * 消防站表正常修改，消防站中间设备表先删除，再插入
     * @param hazardSourcesDevicedVo
     * @return
     */
    @Transactional
    @Override
    public Boolean updatehazardSources(HazardSourcesDevicedVo hazardSourcesDevicedVo){
        //修改
        HazardSources hazardSources=new HazardSources();
        BeanUtils.copyProperties(hazardSourcesDevicedVo,hazardSources);
        hazardSources.setUpdateTime(new Date());
        log.info("消防站修改数据：{}",hazardSources.toString());
        updateById(hazardSources);
        //删除中间设备表数据
       hazardSourcesDeviceService.delByHazardSourcesId(hazardSourcesDevicedVo.getId());
        //写入中间设备表的数据
        String hazardSourcesId=hazardSourcesDevicedVo.getId();
        for(int i=0;i< hazardSourcesDevicedVo.getDevice().size();i++){
            HazardSourcesDevice hazardSourcesDevice=new HazardSourcesDevice();
            hazardSourcesDevice.setDeviceId(hazardSourcesDevicedVo.getDevice().get(i));
            hazardSourcesDevice.setHazardSourcesId(hazardSourcesId);
            hazardSourcesDevice.setType(0);
            hazardSourcesDevice.setId(UUIDUtils.get32UUID());

            hazardSourcesDeviceService.save(hazardSourcesDevice);
        }
        for(int i=0;i<hazardSourcesDevicedVo.getDeviceTradition().size();i++){
            HazardSourcesDevice hazardSourcesDevice=new HazardSourcesDevice();
            hazardSourcesDevice.setDeviceId(hazardSourcesDevicedVo.getDeviceTradition().get(i));
            hazardSourcesDevice.setHazardSourcesId(hazardSourcesId);
            hazardSourcesDevice.setType(1);
            hazardSourcesDevice.setId(UUIDUtils.get32UUID());
            hazardSourcesDeviceService.save(hazardSourcesDevice);
        }

        //修改坐标关系表
        if(StringUtils.isNotBlank(hazardSourcesDevicedVo.getPartitionId())){//判断坐标是否存在分区中
            //在分区
            PartitionsDevice partitionsDevice=new PartitionsDevice();
            partitionsDevice.setId(UUIDUtils.get32UUID());
            partitionsDevice.setType(102);
            partitionsDevice.setDeviceId(hazardSources.getId());
            partitionsDevice.setDeviceName(hazardSources.getName());
            partitionsDevice.setY(hazardSourcesDevicedVo.getLat());
            partitionsDevice.setX(hazardSourcesDevicedVo.getLng());
            partitionsDevice.setPartitionId(hazardSourcesDevicedVo.getPartitionId());
            log.info("插入分区坐标关系表：{}",partitionsDevice.toString());
            int i = partitionsDeviceService.insertPoint(partitionsDevice);
            log.info("分区坐标关系表更新结果：{}",i);
            //删除园区中的坐标
            coordinateRelationService.deleteByTableId(hazardSourcesId, "0");
        }else {
            //不在分区
            //判断是否存在坐标
            List<CoordinateRelation> byTableId = coordinateRelationService.getByTableId(hazardSourcesId, "0");
            if(byTableId.size()>0){//存在
                //根据tableID更新坐标
                boolean result = coordinateRelationService.updateByTableID(hazardSourcesDevicedVo.getLng(), hazardSourcesDevicedVo.getLat(),
                        hazardSourcesId);
                log.info("坐标关系表更新结果：{}", result);
            }else{//不存在插入坐标
                //关系表
                CoordinateRelation coordinateRelation=new CoordinateRelation();
                coordinateRelation.setLng(hazardSourcesDevicedVo.getLng());
                coordinateRelation.setLat(hazardSourcesDevicedVo.getLat());
                coordinateRelation.setTableId(hazardSourcesId);
                coordinateRelation.setId(UUIDUtils.get32UUID());
                coordinateRelation.setUnitId(hazardSourcesDevicedVo.getUnitId());
                coordinateRelation.setTableType("1");
                log.info("插入坐标关系表：{}",coordinateRelation.toString());
                coordinateRelationService.save(coordinateRelation);
            }
            //删除分区中的坐标
            partitionsDeviceService.deletePoint(hazardSourcesDevicedVo.getId());
        }
        return true;


    }


}
